package view.sounds;

import java.net.URL;
import java.util.HashMap;

@SuppressWarnings("unchecked")
public abstract class ResourceCache {

    protected HashMap resources;

    public ResourceCache() {
        resources = new HashMap();
    }

    protected Object loadResource(String name) {
        URL url = null;
        url = getClass().getClassLoader().getResource(name);
        return loadResource(url);
    }
    
    /**
     * Singleton pattern
     * @param name
     * @return
     */
    protected Object getResource(String name) {
        Object res = resources.get(name);
        if (res == null) {
            res = loadResource("sounds/" + name);
            resources.put(name, res);
        }
        return res;
    }

    protected abstract Object loadResource(URL url);
}